//MINI UI解析
mini.parse();

//请求配置处理器类
function EditReqCfgHandler() {
	this.data = {};
	this.name = mini.get("srcName");
	this.request = mini.get("srcRequest");
	this.template = mini.get("srcTemplate");
	this.state = mini.get("srcState");
	this.form = new mini.Form("editReqCfgForm");
	
	this.wmc = new WebMgrConfig();
	
	this.load = function(data) {
		data = mini.clone(data);
		this.data.srcId = data.srcId;
		
		this.initialize();
		
		if (data.srcId) {
			var param = { srcId : data.srcId };
			var json = mini.encode(param);
			$.ajax({
				url : __root + "/getRequestConfig.do", 
				type : "POST", 
				data : { data : json }, 
				dataType : "json", 
				success : function(result) {
					if (result.issuccess) {
						instance.form.setData(result.message);
					} else {
						instance.readonly();
						mini.alert(result.message, "系统提示");
					}
				}, 
				error : function (request) {
					instance.readonly();
					mini.alert(request.responseText, "系统提示");
				}
			});
		};
	};
	
	this.save = function() {
		this.form.validate();
		if (this.form.isValid()) {
			var data = this.form.getData();
			data.srcId = this.data.srcId;
			
			var json = mini.encode(data);
			$.ajax({
				url : __root + "/saveRequestConfig.do", 
				type : "POST", 
				data: {
		        	data: json,
		        	lslUser: gbOperateUser,
		    		lslAddress: gbOperateIp
		        },
				dataType : "json", 
				success : function(result) {
					if (result.issuccess) {
						instance.close("save");
					} else {
						mini.alert(result.message, "系统提示");
					}
				}, 
				error : function (request) {
					mini.alert(request.responseText, "系统提示");
				}
			});
		}
	};
	
	this.close = function(action) {
		if (CloseOwnerWindow) {
			CloseOwnerWindow(action);
		} else {
			window.close();
		}
	};
	
	this.initialize = function() {
		this.template.load(__root + "/getTemplates.do");
		
		var stateData = mini.clone(this.wmc.normalState);
		stateData.removeAt(0);
		this.state.setData(stateData);
	};
	
	this.readonly = function() {
		this.name.readOnly = true;
		this.request.readOnly = true;
		this.template.readOnly = true;
		this.state.readOnly = true;
	};
};

//请求配置处理器全局实例
var instance = new EditReqCfgHandler();

//定义模板树节点选择前事件处理程序
function onSrcTemplateBeforeNodeSelect(e) {
	if (e.isLeaf == false) e.cancel = true;
}

//定义保存按钮点击事件处理程序
function onSaveClick(e) {
	instance.save();
}

//定义关闭按钮点击事件处理程序
function onCancelClick(e) {
	instance.close("cancel");
}
